/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.flexengine.component;

import static com.flexengine.component.GamePanel.BASIC_BORDER_COLOR;
import com.flexengine.engine.Priority;
import com.flexengine.input.InputManager;
import com.flexengine.utils.FontManager;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Luann R. Athayde
 * @version 1.0
 * @since 1.0
 */
public class GameConsole extends GamePanel {

    private final List<ConsoleListener> listeners;

    protected volatile Label   topLabel;
    protected volatile Button  buttonClose;
    private final TextField textFieldConsole;
    private final TextField textFieldCommand;

    public GameConsole() {
        super("Console", 100, 100, new Dimension(480, 320));
        Color corBorda = new Color(160, 160, 180);
        Color corTexto = new Color(0,200,240);
        // -- basic settings...
        setPriority(Priority.HIGH_PRIORITY);
        //this.setDragArea(0, 0, 480, 32);

        listeners = new ArrayList<>();

        // -- Panel bar...
        topLabel = new Label("    Console");
        topLabel.setBounds(0, 0, 448, 32);
        topLabel.setBorder(BASIC_BORDER_COLOR, 2);
        topLabel.setFont(FontManager.getFont("Serif", 1, 16));
        topLabel.setBackgroundColor(Color.BLACK);
        topLabel.setForegroundColor(Color.WHITE);
        topLabel.setTransparence(0.5f);
        // --
        buttonClose = new Button("X");
        buttonClose.setBounds(448, 0, 32, 32);
        buttonClose.setRoundRect(Button.ROUND_RECT_NONE);
        buttonClose.setBorder(BASIC_BORDER_COLOR, 2);
        buttonClose.setFont(FontManager.getFont("Serif", 1, 14));
        buttonClose.setBackgroundColor(Color.BLACK);
        buttonClose.setForegroundColor(Color.WHITE);
        buttonClose.setTransparence(0.5f);
        buttonClose.addEventListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
            }
        });
        
        textFieldConsole = new TextField();
        textFieldConsole.setFont(FontManager.getFont("Serif", 1, 14));
        textFieldConsole.setBackgroundColor(Color.BLACK);
        textFieldConsole.setBorderColor(corBorda);
        textFieldConsole.setBorderSize(2);
        textFieldConsole.setForegroundColor(corTexto);
        textFieldConsole.setCursorColor(corTexto);
        textFieldConsole.setBounds(0f, 32f, 480, 320 - 64);
        textFieldConsole.setSelected(false);
        textFieldConsole.setTransparence(0.5f);

        textFieldCommand = new TextField();
        textFieldCommand.setFont(FontManager.getFont("Serif", 1, 14));
        textFieldCommand.setBackgroundColor(Color.BLACK);
        textFieldCommand.setBorderColor(corBorda);
        textFieldCommand.setBorderSize(2);
        textFieldCommand.setForegroundColor(corTexto);
        textFieldCommand.setCursorColor(corTexto);
        textFieldCommand.setBounds(0f, 320f - 32f, 480, 32);
        textFieldCommand.setSelected(false);
        textFieldCommand.setTransparence(0.5f);

        add(topLabel);
        add(buttonClose);
        add(textFieldConsole);
        add(textFieldCommand);
        setVisible(false);

        textFieldCommand.addEventListener((ActionEvent e) -> {
            executeCommand();
        });
    }

    private void executeCommand() {
        listeners.stream().forEach((listener) -> {
            listener.execute(this, textFieldCommand.getText());
        });
        textFieldCommand.setText("");
    }

    @Override
    public void update(Long time, Long totalTime) {
        super.update(time, totalTime);
        if ( InputManager.isKeyDown(InputManager.VK_CONTROL) && InputManager.isKeyDown(InputManager.VK_C)) {
            setVisible(!isVisible());
            InputManager.releaseKey(InputManager.VK_C);
        }
    }

    public boolean addConsoleListener(ConsoleListener listener) {
        return listeners.add(listener);
    }

    public boolean removeConsoleListener(ConsoleListener listener) {
        return listeners.remove(listener);
    }

    public TextField getTextFieldCommand() {
        return textFieldCommand;
    }

    public TextField getTextFieldConsole() {
        return textFieldConsole;
    }

    /**
     * Console Listener (interface)
     */
    public interface ConsoleListener {

        /**
         * Execute a command typed on console.
         *
         * @param console
         * @param command
         */
        public void execute(GameConsole console, String command);

    }

}
